package lumis.content.source.field;

import lumis.doui.source.field.type.ISourceFieldDataType;
import lumis.doui.table.field.SingleColumnTableSourceField;
import lumis.portal.PortalException;
import lumis.util.XmlUtil;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

import org.w3c.dom.Node;

/**
 * A generic meta data source field.
 * @since 4.0.4
 * @version $Revision: 12383 $ $Date: 2011-01-18 14:34:41 -0200 (Tue, 18 Jan 2011) $
 */
public class MetaDataSourceField extends SingleColumnTableSourceField
{
	private static ILogger logger = LoggerFactory.getLogger(MetaDataSourceField.class);

	public static enum Type
	{
		WorkflowState, 
		WorkflowAssignedTo, 
		ServiceInstanceId, 
		PublishToServiceInstances, 
		PublishStartDate, 
		PublishEndDate, 
		HighlightEndDate, 
		Highlight, 
		Comments, 
		PublishToPrincipals, 
		Language, 
		LastModifiedDateTime, 
		CreatedDateTime, 
		VersionContentId, 
		VersionNumber, 
		VersionActive,
		Association, 
		ContentId, 
		Lock,
		Tag,
		None
	}
	
	public MetaDataSourceField(String tableRelationId, String columnName, String dataTypeId) throws PortalException	
	{
		super(tableRelationId, columnName, dataTypeId);
	}

	public MetaDataSourceField(String tableRelationId, String columnName, ISourceFieldDataType dataType)
	{
		super(tableRelationId, columnName, dataType);
	}

	@Override
	public Node getDefaultInputControlDefinition() throws PortalException
	{
		String controlDefinition;
		if (XmlUtil.readAttributeBoolean("isPublishToServiceInstances", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_publishToServiceInstances\" />";
		}
		else if (XmlUtil.readAttributeBoolean("isPublishToPrincipals", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_publishToPrincipals\" />";
		}
		else if (XmlUtil.readAttributeBoolean("isLastModifiedDateTime", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_text\" id=\"" + getId() + "\"  dataId=\"" + getId() + "\"/>";
		}
		else if (XmlUtil.readAttributeBoolean("isCreatedDateTime", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_text\" id=\"" + getId() + "\"  dataId=\"" + getId() + "\" />";
		}
		else if (XmlUtil.readAttributeBoolean("isComments", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_comments\" id=\"" + getId() + "\"  dataId=\"" + getId() + "\" />";
		}
		else if (XmlUtil.readAttributeBoolean("isLanguage", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_language\" id=\"" + getId() + "\"  dataId=\"" + getId() + "\" />";
		}
		else if (XmlUtil.readAttributeBoolean("isAssociation", definitionNode, false))
		{
			controlDefinition = "<control type=\"lum_hidden\" id=\"" + getId() + "\"  dataId=\"" + getId() + "\" />";
		}
		else
		{
			return super.getDefaultInputControlDefinition();
		}
		
		return XmlUtil.getDocument(controlDefinition).getDocumentElement();
	}
	
	public Type getType()
	{
  		try
		{
			if (XmlUtil.readAttributeBoolean("isWorkflowState", definitionNode, false))
				return MetaDataSourceField.Type.WorkflowState;
			else if (XmlUtil.readAttributeBoolean("isWorkflowAssignedTo", definitionNode, false))
				return MetaDataSourceField.Type.WorkflowAssignedTo;
			else if (XmlUtil.readAttributeBoolean("isServiceInstanceId", definitionNode, false))
				return MetaDataSourceField.Type.ServiceInstanceId;
			else if (XmlUtil.readAttributeBoolean("isPublishToServiceInstances", definitionNode, false))
				return MetaDataSourceField.Type.PublishToServiceInstances;
			else if (XmlUtil.readAttributeBoolean("isPublishStartDate", definitionNode, false))
				return MetaDataSourceField.Type.PublishStartDate;
			else if (XmlUtil.readAttributeBoolean("isPublishEndDate", definitionNode, false))
				return MetaDataSourceField.Type.PublishEndDate;
			else if (XmlUtil.readAttributeBoolean("isHighlightEndDate", definitionNode, false))
				return MetaDataSourceField.Type.HighlightEndDate;
			else if (XmlUtil.readAttributeBoolean("isHighlight", definitionNode, false))
				return MetaDataSourceField.Type.Highlight;
			else if (XmlUtil.readAttributeBoolean("isComments", definitionNode, false))
				return MetaDataSourceField.Type.Comments;
			else if (XmlUtil.readAttributeBoolean("isPublishToPrincipals", definitionNode, false))
				return MetaDataSourceField.Type.PublishToPrincipals;
			else if (XmlUtil.readAttributeBoolean("isLanguage", definitionNode, false))
				return MetaDataSourceField.Type.Language;
			else if (XmlUtil.readAttributeBoolean("isLastModifiedDateTime", definitionNode, false))
				return MetaDataSourceField.Type.LastModifiedDateTime;
			else if (XmlUtil.readAttributeBoolean("isCreatedDateTime", definitionNode, false))
				return MetaDataSourceField.Type.CreatedDateTime;
			else if (XmlUtil.readAttributeBoolean("isVersionContentId", definitionNode, false))
				return MetaDataSourceField.Type.VersionContentId;
			else if (XmlUtil.readAttributeBoolean("isVersionNumber", definitionNode, false))
				return MetaDataSourceField.Type.VersionNumber;
			else if (XmlUtil.readAttributeBoolean("isVersionActive", definitionNode, false))
				return MetaDataSourceField.Type.VersionActive;
			else if (XmlUtil.readAttributeBoolean("isAssociation", definitionNode, false))
				return MetaDataSourceField.Type.Association;
			else if (XmlUtil.readAttributeBoolean("isContentId", definitionNode, false))
				return MetaDataSourceField.Type.ContentId;
			else if(XmlUtil.readAttributeBoolean("isLock", definitionNode, false))
				return MetaDataSourceField.Type.Lock;
			else if(XmlUtil.readAttributeBoolean("isTag", definitionNode, false))
				return MetaDataSourceField.Type.Tag;
			else
				return MetaDataSourceField.Type.None;
		}
		catch (PortalException e)
		{
			logger.error("Exception thrown in MetaDataSourceField.getType", e);
			return MetaDataSourceField.Type.None;
		}
	}
}
